;;; This file is part of guix-bavier.git
;;; Copyright © 2024 Eric Bavier <bavier@posteo.net>
;;; License: GPLv3+

(define-module (bavier packages algebra)
  #:use-module (guix packages)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system copy)
  #:use-module (guix build-system gnu)
  #:use-module (guix git-download)
  #:use-module (guix gexp)
  #:use-module (gnu packages)
  #:use-module (gnu packages algebra)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages maths)
  #:use-module (gnu packages multiprecision)
  #:use-module ((guix licenses) #:prefix license:))

(define-public qsopt-exact
  (let ((commit "e5d498fde468e4669a3fbc4736e5d3b878e8c148")
        (revision "0"))
    (package
     (name "qsopt-exact")
     (version (git-version "2.5.10.3" revision commit))
     (home-page "https://github.com/jonls/qsopt-ex")
     (source
      (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit commit)))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "050ia659vxpdf69kp3qy0cxrvl97zmdj66d8das92ixb77qfz25p"))))
     (build-system gnu-build-system)
     (native-inputs (list autoconf automake libtool))
     (inputs (list gmp zlib bzip2))
     (synopsis "Exact linear programming solver")
     (description
      "The QSopt code is based on floating-point arithmetic and the results are
subject to rounding errors, as is the case with other commerical and academic
linear-programming solvers. For the exact solution of LP problems, we have
created the QSopt_ex rational solver and callable library. This package makes
use of the GNU MP Bignum Library to deliver the exact rational solution to LP
instances having rational input.

This is a fork of QSopt_ex by Daniel Espinoza et al. version 2.5.10.")
     (license (list license:gpl3+ license:lgpl2.1+)))))

(define-public mpreal
  (package
    (name "mpreal")
    (version "3.7.1")
    (home-page "https://github.com/advanpix/mpreal")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit (string-append "mpfrc++-" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "0xk8i6cyfkjml0h4ylbd7v8y3w1brhhwfvskz2cjkv4r70vfp2pi"))))
    (build-system copy-build-system)
    (arguments
     `(#:install-plan
       '(("mpreal.h" "include/"))))
    (synopsis "Multi-precision floating point number class for C++")
    (description
     "MPFR C++ (MPREAL) is a high-performance C++ interface for the MPFR
library. MPFR allows user to conduct floating-point calculations with
virtually any (restricted by available memory only) precision with correct
rounding.  Besides simple arithmetic operations like “+” and “/” the whole set
of mathematical functions is supported: @code{sin}, @code{sqrt}, @code{pow},
@code{log}, etc.")
    (license license:gpl3+)))

(define-public rminimax
  (let ((commit "46e1123cd9b842f39c7f1ec5ae7c08938ce70961")
        (revision "0"))
    (package
     (name "rminimax")
     (version (git-version "0" revision commit)) ; No official release yet
     (home-page "https://gitlab.inria.fr/sfilip/rminimax")
     (source
      (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit commit)))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "0wdviy75jbdvs4g95mw1ap33bq5w14daibwxglm06ckl95gmnxxs"))
       (patches (search-patches
                 "bavier/patches/rminimax-install-bin.patch"))))
     (build-system cmake-build-system)
     (outputs '("out" "debug"))
     (inputs
      (list gnuplot
            eigen
            gmp
            mpfr
            mpreal
            fplll
            flint
            qsopt-exact))
     (arguments
      (list
       #:build-type "Debug"
       #:tests? #f                      ; `./test_atan` takes a long time
       #:phases
       #~(modify-phases %standard-phases
           (add-after 'unpack 'fix-gnuplot
             (lambda _
               (substitute* "src/plotting.cpp"
                 (("\"gnuplot ")
                  (string-append "\"" (which "gnuplot") " "))))))))
     (synopsis "Polynomial and rational approximations to math functions")
     (description
      "The @code{rminimax} project provides tools for designing flexible polynomial
and rational approximations to mathematical functions.  It can be used as a
library, but also provides an executable that parses command line arguments to
compute generalized rational approximations.")
     (license license:gpl3+))))
